import java.util.*;


public class Board {
	
	static char BLACK = 'B';
	static char WHITE = 'W';
	static char EMPTY = '-';
	static char DEAD = 'X';
	
	public char[][] board;
	public int size;
	
	public void create() {
		Scanner scan = new Scanner(System.in);
		scan.useDelimiter("\\z");
		String input = scan.next();
		
		size = Integer.parseInt(input.substring(0,1));
		board = new char[size][size];
		
		input = input.replaceAll("\\s", "");
		input = input.substring(1);
		
		if (input.length() != size*size) {
			System.err.println("Error: invalid input size");
			System.exit(0);
		}
		
		for (int i = 0; i < size; i++) {
			for (int j = 0; j < size; j++) {
				if (input.charAt(i*size+j) != BLACK && input.charAt(i*size+j) != WHITE &&
						input.charAt(i*size+j) != EMPTY && input.charAt(i*size+j) != DEAD) {
					System.err.println("Error: invalid board input");
					System.exit(0);
				}
				else
				board[i][j] = input.charAt(i*size+j);
			}
		}
		
	}
	
	public void insert(int row, int col, char value) {
		board[row][col] = value;
	}
	
	public void remove(int row, int col) {
		board[row][col] = EMPTY;
	}
	
	public void printOut() {
		for (int i=0; i<size; i++) {
			for (int j=0; j<size; j++) {
				System.out.print(board[i][j]);
			}
			System.out.println();
		}
	}
	
	public int numJumpMoves(char player) {
		AdjList list = new AdjList();
		
		for (int row = 0; row < size; row++) {
			for (int col = 0; col < size; col++) {
				if (board[row][col] == player)
					jumpMoves(list, row, col, player);
			}
		}
		
		return list.numEdges();
	}

	public int numEmpty() {
		int emptyAmount = 0;
		
		for (int row = 0; row < size; row++) {
			for (int column = 0; column < size; column++) {
				if (board[row][column] == EMPTY)
					emptyAmount++;
			}
		}
		
		return emptyAmount;
	}
	
	private boolean jumpMoves (AdjList list, int row, int col, char player) {
		
		if (list.doesExist(row, col)) {
			return false;
		}
		
		if (canJump(list, row, col, row-1, col-1, row-2, col-2)) {
			if (list.doesExist(row-2, col-2) == false) {
				insert(row-2, col-2, player);
				jumpMoves(list, row-2, col-2, player);
				remove(row-2, col-2);
			}
		}
		if (canJump(list, row, col, row-1, col, row-2, col)) {
			if (list.doesExist(row-2, col) == false) {
				insert(row-2, col, player);
				jumpMoves(list, row-2, col, player);
				remove(row-2, col);
			}
		}
		if (canJump(list, row, col, row-1, col+1, row-2, col+2)) {
			if (list.doesExist(row-2, col+2) == false) {
				insert(row-2, col+2, player);
				jumpMoves(list, row-2, col+2, player);
				remove(row-2, col+2);
			}
		}
		if (canJump(list, row, col, row, col-1, row, col-2)) {
			if (list.doesExist(row, col-2) == false) {
				insert(row, col-2, player);
				jumpMoves(list, row, col-2, player);
				remove(row, col-2);
			}
		}
		if (canJump(list, row, col, row, col+1, row, col+2)) {
			if (list.doesExist(row, col+2) == false) {
				insert(row, col+2, player);
				jumpMoves(list, row, col+2, player);
				remove(row, col+2);
			}
		}
		if (canJump(list, row, col, row+1, col-1, row+2, col-2)) {
			if (list.doesExist(row+2, col-2) == false) {
				insert(row+2, col-2, player);
				jumpMoves(list, row+2, col-2, player);
				remove(row+2, col-2);
			}
		}
		if (canJump(list, row, col, row+1, col, row+2, col)) {
			if (list.doesExist(row+2, col) == false) {
				insert(row+2, col, player);
				jumpMoves(list, row+2, col, player);
				remove(row+2, col);
			}
		}
		if (canJump(list, row, col, row+1, col+1, row+2, col+2)) {
			if (list.doesExist(row+2, col+2) == false) {
				insert(row+2, col+2, player);
				jumpMoves(list, row+2, col+2, player);
				remove(row+2, col+2);
			}
		}
		return true;
	}
	
	private boolean canJump (AdjList list, int r1, int c1, int r2, int c2, int r3, 
			int c3) {
		
		if (r3 < 0 || r3 >= size || c3 < 0 || c3 >= size)
			return false; // (r3, c3) is off the board
		
		if (r2 < 0 || r2 >= size || c2 < 0 || c2 >= size)
			return false; // (r2, c2) is off the board
		
		if (board[r3][c3] != EMPTY)
			return false; // (r3, c3) already contains a piece
		
		if (board[r2][c2] == EMPTY || board[r2][c2] == DEAD)
			return false;
		
		list.insert(r1, c1);
		list.insertNeighbour(r1, c1, r2, c2);
		return true;
	}
}


